import { ref, computed, reactive, toRaw, unref } from "vue";
import type { ComputedRef, CSSProperties } from "vue";
import type { IDragBoxProps, IPositon } from "../types";

type ActionType = {
  parentPosition: IPositon;
  boxMsg: any;
};
export function useMove(
  props: ComputedRef<IDragBoxProps>,
  { parentPosition, boxMsg }: ActionType
) {
  const minX = computed(() => {
    // parentPosition
    const { parent } = unref(props);
    return parent ? 0 : -Infinity;
  });
  const maxX = computed(() => {
    // parentPosition
    const { parent } = unref(props);
    return parent ? parentPosition.w - boxMsg.w : Infinity;
  });
  const minY = computed(() => {
    const { parent } = unref(props);
    return parent ? 0 : -Infinity;
  });
  const maxY = computed(() => {
    const { parent } = unref(props);
    return parent ? parentPosition.h - boxMsg.h : Infinity;
  });
  const minW = computed(() => {
    // const currentMaxW = parentPosition.w - boxMsg.x;
    return Math.min(boxMsg.minW, boxMsg.maxW);
  });
  const maxW = computed(() => {
    const { parent } = unref(props);
    return parent ? Math.max(boxMsg.minW, boxMsg.maxW) : Infinity;
  });
  const minH = computed(() => {
    return Math.min(boxMsg.minH, boxMsg.maxH);
  });
  const maxH = computed(() => {
    const { parent } = unref(props);
    return parent ? Math.max(0, boxMsg.minH, boxMsg.maxH) : Infinity;
  });
  const setX = (value: number) => {
    boxMsg.x = Math.min(Math.max(minX.value, value), maxX.value);
  };
  const setY = (value: number) => {
    boxMsg.y = Math.min(Math.max(minY.value, value), maxY.value);
  };
  const setH = (value: number) => {
    const { parent } = unref(props);
    const currentMaxH = parentPosition.h - boxMsg.y;
    if (parent) {
      if (boxMsg.y <= 0) return;
      boxMsg.h = Math.min(Math.max(minH.value, value), maxH.value, currentMaxH);
    } else {
      boxMsg.h = Math.min(Math.max(minH.value, value), maxH.value);
    }
  };

  const setW = (value: number) => {
    const { parent } = unref(props);
    const currentMaxW = parentPosition.w - boxMsg.x;
    if (parent) {
      if (boxMsg.x <= 0) {
        return;
      }
      boxMsg.w = Math.min(Math.max(minW.value, value), maxW.value, currentMaxW);
    } else {
      boxMsg.w = Math.min(Math.max(minW.value, value), maxW.value);
    }
  };
  return {
    setX,
    setY,
    setH,
    setW
  };
}
